package com.rox.el;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;




public class MainR{

	private static final long serialVersionUID = 1L;
	private JLabel bgWord;
	private JTextField engWord;
	private JButton enterButton;
	private JButton nextButton;
	private static final int WIDTH = 320;
	private static final int HEIGHT = 224;
	private Dimension size = new Dimension(WIDTH, HEIGHT);
	private Font font = new Font("Cambria", Font.PLAIN, 20);
	private Dimension vi = new Dimension(200,20);
	private ArrayList<EnBg> cardList;
	private EnBg currentCard;
	private int currentCardIndex = 0;
	private JPanel b;
	private int imb= 0;
	private boolean hox = true;
	private final int NOPE = -1;
	private final int NORMAL = 0;
	private final int NEED = 1;
	private final int MUST = 2;
	private final int BR = 4;
	private int[] scale;
	int randomI;
	private JButton done;
	JPanel p2;
	JFrame frame;
	private ArrayList<String> wrongEn = new ArrayList<String>();
	private ArrayList<String> wrongBg = new ArrayList<String>();
	
	//public static void main(String[] args){
	//	new MainR();
	//}
	
	public MainR(){

		loadCard();
		makeScale();
		frame = new JFrame();
		frame.setTitle("Random");
		int randomI = (int)(Math.random()*cardList.size());
		currentCard = cardList.get(randomI);
		currentCardIndex = randomI;
		JPanel p = new JPanel();
		p.setBackground(Color.WHITE);
		p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
		JLabel theWord = new JLabel("The word is: ");
		theWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		theWord.setSize(theWord.getPreferredSize());
		theWord.setFont(font);
		frame.add(p, BorderLayout.CENTER);
		
		bgWord = new JLabel(currentCard.bgWord);
		bgWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		bgWord.setSize(theWord.getPreferredSize());
		bgWord.setFont(font);
		
		
		engWord = new JTextField();
		engWord.addActionListener(new BgListener());
		engWord.setFont(font);
		engWord.setBackground(Color.lightGray);
		engWord.setMaximumSize(vi);
		engWord.setHorizontalAlignment(JTextField.CENTER);
		engWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		p2 = new JPanel();
		p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
		
		enterButton = new JButton("Enter");
		enterButton.setMaximumSize(new Dimension(78,35));
		enterButton.setFont(new Font("Cambria", Font.PLAIN, 16));
		enterButton.setBorderPainted(false);
		enterButton.setFocusPainted(false);
		enterButton.setBackground(new Color(205,205,205));
		enterButton.addActionListener(new EnterListener());
		
		nextButton = new JButton("Next");
		nextButton.setMaximumSize(new Dimension(78,35));
		nextButton.setFont(new Font("Cambria", Font.PLAIN, 16));
		nextButton.setBorderPainted(false);
		nextButton.setFocusPainted(false);
		nextButton.setBackground(new Color(205,205,205));
		nextButton.addActionListener(new NextListener());
		nextButton.setVisible(false);
		
		done = new JButton("Done");
		done.setMaximumSize(new Dimension(78,35));
		done.setFont(new Font("Cambria", Font.PLAIN, 16));
		done.setBorderPainted(false);
		done.setFocusPainted(false);
		done.setBackground(new Color(205,205,205));
		done.addActionListener(new DoneListener());
		done.setVisible(false);
		
		p.add(theWord);
		p.add(bgWord);
		p.add(Box.createRigidArea(new Dimension(0, 30)));
		p.add(engWord);
		p.add(Box.createRigidArea(new Dimension(0, 35)));
		//p.add(enterButton);
		p2.add(enterButton);
		//p2.add(Box.createRigidArea(new Dimension(25,0)));
		p2.add(nextButton);
		p2.add(done);
		p.add(p2);
		b = new JPanel();
		b.setMaximumSize(new Dimension(25,25));
		b.setBackground(Color.WHITE);
		p.add(b);
		frame.setSize(size);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		
		
	}
	/*	
	private final int NOPE = -1;
	private final int NORMAL = 0;
	private final int NEED = 1;
	private final int MUST = 2;
	private final int BR = 4;
	private int[] scale;*/
	
	private void makeScale(){
		
		scale = new int[cardList.size()];
		for(int i = 0; i < scale.length; i++){
			scale[i] = NORMAL;
		}
		
	}
	
	private void loadCard(){
		cardList = new ArrayList<EnBg>();
		try{
			File myFile = new File("res/bat.txt");
			BufferedReader reader = new BufferedReader(new FileReader(myFile));
			String line = null; //you can combine this with a while loop (while line != null)
			while((line = reader.readLine()) != null){
				
				String[] result = line.split("/");
				EnBg card = new EnBg(result[0],result[1]);
				cardList.add(card);
				
			}
			reader.close();
			//You can also combine it with (String [] result = lineToParse.split("/");)
		}catch(Exception ex){ex.printStackTrace();}
	}
	
	class BgListener implements ActionListener{

		//WIDTH = 320;
		//HEIGHT = 224;
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(hox == true){
				if (engWord.getText().equalsIgnoreCase(currentCard.trueWord)){
					nextButton.setVisible(true);
					done.setVisible(true);
					imb = 0;
					hox = false;
					enterButton.setVisible(false);
					b.setBackground(Color.GREEN);
					if(scale[randomI] == NORMAL || scale[randomI] == NEED || scale[randomI] == MUST || scale[randomI] == BR){
						scale[randomI] -= 1;
					}
					
				}else if(engWord.getText().equalsIgnoreCase(currentCard.trueWord)){
					enterButton.setEnabled(false);
					enterButton.setText("No more");
					b.setBackground(Color.WHITE);
					
				}else{
					b.setBackground(new Color(255/(imb + 1),0,0));
					imb++;
				}
				if (imb > 2){
					engWord.setText(engWord.getText() + " - " + currentCard.trueWord);
					wrongEn.add(currentCard.trueWord);
					wrongBg.add(currentCard.bgWord);
					if(scale[randomI] == NOPE || scale[randomI] == NORMAL || scale[randomI] == NEED || scale[randomI] == MUST){
						scale[randomI] += 1;
					}
					imb = 0;
					if(currentCardIndex == cardList.size()){
						enterButton.setEnabled(false);
						enterButton.setText("No more");
						b.setBackground(Color.WHITE);
					}else{
					nextButton.setVisible(true);
					done.setVisible(true);
					imb = 0;
					hox = false;
					enterButton.setVisible(false);
					b.setBackground(Color.WHITE);
					}
				}
			}else{
				/*	
				private final int NOPE = -1;
				private final int NORMAL = 0;
				private final int NEED = 1;
				private final int MUST = 2;
				private final int BR = 4;
				private int[] scale;*/
				nextButton.setVisible(false);
				done.setVisible(false);
				enterButton.setVisible(true);
				imb = 0;
				hox = true;
				engWord.requestFocus();
				boolean done = false;
				while(done == false){
					randomI = (int)(Math.random()*cardList.size());
					if(scale[randomI] == NOPE){
						int ranN = (int)(Math.random()*10) + 1;
						if(ranN == 5){
							currentCard = cardList.get(randomI);
							currentCardIndex = randomI;
							done = true;
						}
					}else if(scale[randomI] == NORMAL){
						int ranN = (int)(Math.random()*6) + 1;
						if(ranN == 3){
							currentCard = cardList.get(randomI);
							currentCardIndex = randomI;
							done = true;
						}
					}else if(scale[randomI] == NEED){
						int ranN = (int)(Math.random()*4) + 1;
						if(ranN == 2){
							currentCard = cardList.get(randomI);
							currentCardIndex = randomI;
							done = true;
						}
					}else if(scale[randomI] == MUST){
						int ranN = (int)(Math.random()*2) + 1;
						if(ranN == 1){
							currentCard = cardList.get(randomI);
							currentCardIndex = randomI;
							done = true;
						}
					}else{
						currentCard = cardList.get(randomI);
						done = true;
						//currentCardIndex = randomI;
					}
				}
				
				engWord.setText("");
				bgWord.setText(currentCard.bgWord);
				b.setBackground(Color.WHITE);
			}
		}
		
	}
	class EnterListener implements ActionListener{

		//WIDTH = 320;.equalsIgnoreCase(physicist2)
		//HEIGHT = 224;
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (engWord.getText().equalsIgnoreCase(currentCard.trueWord)){
				nextButton.setVisible(true);
				done.setVisible(true);
				imb = 0;
				hox = false;
				enterButton.setVisible(false);
				b.setBackground(Color.GREEN);
				if(scale[randomI] == NORMAL || scale[randomI] == NEED || scale[randomI] == MUST || scale[randomI] == BR){
					scale[randomI] -= 1;
				}
				
			}else if(engWord.getText().equalsIgnoreCase(currentCard.trueWord)){
				enterButton.setEnabled(false);
				enterButton.setText("No more");
				b.setBackground(Color.WHITE);
				
			}else{
				b.setBackground(new Color(255/(imb + 1),0,0));
				imb++;
			}
			if (imb > 2){
				engWord.setText(engWord.getText() + " - " + currentCard.trueWord);
				wrongEn.add(currentCard.trueWord);
				wrongBg.add(currentCard.bgWord);
				if(scale[randomI] == NOPE || scale[randomI] == NORMAL || scale[randomI] == NEED || scale[randomI] == MUST){
					scale[randomI] += 1;
				}
				imb = 0;
				if(currentCardIndex == cardList.size()){
					enterButton.setEnabled(false);
					enterButton.setText("No more");
					b.setBackground(Color.WHITE);
				}else{
				nextButton.setVisible(true);
				done.setVisible(true);
				imb = 0;
				hox = false;
				enterButton.setVisible(false);
				b.setBackground(Color.WHITE);
				}
			}
		}
		
	}
	class DoneListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String text;
			int j = 0;
			new Wrong();
			Wrong.wrongWords.setText("");
			for(int i = 0; i<wrongEn.size(); i++){
				text = Wrong.wrongWords.getText();
				if(!text.contains(wrongEn.get(i)) || (!text.contains((wrongBg.get(i))))){
				j++;
				Wrong.wrongWords.append(j + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
				}else continue;
			}
			frame.setVisible(false);
			
		}
		
	}
	class NextListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			nextButton.setVisible(false);
			done.setVisible(false);
			enterButton.setVisible(true);
			imb = 0;
			hox = true;
			engWord.requestFocus();
			boolean done = false;
			while(done == false){
				randomI = (int)(Math.random()*cardList.size());
				if(scale[randomI] == NOPE){
					int ranN = (int)(Math.random()*15) + 1;
					if(ranN == 5){
						currentCard = cardList.get(randomI);
						currentCardIndex = randomI;
						done = true;
					}
				}else if(scale[randomI] == NORMAL){
					int ranN = (int)(Math.random()*6) + 1;
					if(ranN == 3){
						currentCard = cardList.get(randomI);
						currentCardIndex = randomI;
						done = true;
					}
				}else if(scale[randomI] == NEED){
					int ranN = (int)(Math.random()*4) + 1;
					if(ranN == 2){
						currentCard = cardList.get(randomI);
						currentCardIndex = randomI;
						done = true;
					}
				}else if(scale[randomI] == MUST){
					int ranN = (int)(Math.random()*2) + 1;
					if(ranN == 1){
						currentCard = cardList.get(randomI);
						currentCardIndex = randomI;
						done = true;
					}
				}else{
					currentCard = cardList.get(randomI);
					done = true;
					//currentCardIndex = randomI;
				}
			}
			
			engWord.setText("");
			bgWord.setText(currentCard.bgWord);
			b.setBackground(Color.WHITE);
			}
		
	}
	
	
	
}
